/*
 * HMWorldCoordinatesDialog.java
 */

package itesm.gvision.apps.calibrator;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

//----------------------------------------------------------------------------------
/**
 * Dialogo para la especificacion de los puntos de correspondencia imagen <--> mundo.
 * @author  hugo
 */
public class HMWorldCoordinatesDialog extends javax.swing.JDialog 
{
	private boolean ok;
	
	private double xWorld, yWorld;
	
	private double xImage, yImage;
	
	private CalibratorHomographyImagePanel chip = new CalibratorHomographyImagePanel();
	
	private Rectangle subImage;
	
	private int scale;
	
	//----------------------------------------------------------------------------------
	/** 
	 * Creates new form HMWorldCoordinatesDialog 
	 * @param parent @inherit
	 * @param modal @inherit
	 * @param imgPnt El punto de la imagen
	 * @param wrldPnt El punto en el mundo real
	 * @param sub Lugar y tama�o de la sub-imagen
	 * @param scale Factor de escala para los pixeles
	 * @param w Ancho de la image original
	 * @param h Alto de la imagen original
	 * @param img Imagen con zoom
	 */
	public HMWorldCoordinatesDialog(java.awt.Frame parent, boolean modal, Point2D imgPnt, Point2D wrldPnt, Rectangle sub, int scale, BufferedImage img, int w, int h) 
	{
		super(parent, modal);
		initComponents();
		this.lblImageCoord.setText("(" + imgPnt.getX() + ", " + imgPnt.getY() + ")");
		this.eWorldX.setText(Double.toString(wrldPnt.getX()));
		this.eWorldY.setText(Double.toString(wrldPnt.getY()));
		this.xWorld = wrldPnt.getX();
		this.yWorld = wrldPnt.getY();
		this.xImage = imgPnt.getX();
		this.yImage = imgPnt.getY();
		this.pnlZoomedImage.add(this.chip);
		if (sub.x < 0){
			sub.x = 0;
		}
		else if (sub.x + sub.width > w){
			sub.x = w - sub.width;
		}
		if (sub.y < 0){
			sub.y = 0;
		}
		else if (sub.y + sub.height > h){
			sub.y = h - sub.height;
		}
		this.subImage = sub;
		this.chip.setImage(img);
		this.chip.addPoint(new Point((int)(Math.abs(sub.x-imgPnt.getX())*scale), (int)(Math.abs(sub.y-imgPnt.getY())*scale)));
		this.scale = scale;
		
	}

	//----------------------------------------------------------------------------------
	/**
	 * Transforma la coordenada (x,y) en la referencia del componente a la coordenada
	 * real en la subimagen de acuerdo al factor de escalamiento.
	 */
	private Point2D getZoomedPoint(int x, int y)
	{
		Point2D res = new Point2D.Double();
		double nx = this.subImage.getX() + (double)x/this.scale;
		double ny = this.subImage.getY() + (double)y/this.scale;
		res.setLocation(nx, ny);
		return res;
	}
	
	//----------------------------------------------------------------------------------
	public boolean getResult()
	{
		return this.ok;
	}
	
	//----------------------------------------------------------------------------------
	public double getWorldX()
	{
		return this.xWorld;
	}
	
	//----------------------------------------------------------------------------------
	public double getWorldY()
	{
		return this.yWorld;
	}

	//----------------------------------------------------------------------------------
	public double getImageX()
	{
		return this.xImage;
	}
	
	//----------------------------------------------------------------------------------
	public double getImageY()
	{
		return this.yImage;
	}
	
	//----------------------------------------------------------------------------------
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
     private void initComponents() {//GEN-BEGIN:initComponents
          pnlZoomedImage = new javax.swing.JPanel();
          jPanel1 = new javax.swing.JPanel();
          lblImageCoord = new javax.swing.JLabel();
          jLabel1 = new javax.swing.JLabel();
          eWorldX = new javax.swing.JTextField();
          eWorldY = new javax.swing.JTextField();
          jPanel2 = new javax.swing.JPanel();
          btnOK = new javax.swing.JButton();
          btnCancel = new javax.swing.JButton();

          getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

          setTitle("Add a point Image <==> World reference frame");
          addWindowListener(new java.awt.event.WindowAdapter() {
               public void windowClosing(java.awt.event.WindowEvent evt) {
                    closeDialog(evt);
               }
          });

          pnlZoomedImage.setLayout(new java.awt.BorderLayout());

          pnlZoomedImage.addMouseListener(new java.awt.event.MouseAdapter() {
               public void mouseClicked(java.awt.event.MouseEvent evt) {
                    pnlZoomedImageMouseClicked(evt);
               }
          });

          getContentPane().add(pnlZoomedImage);

          jPanel1.setBorder(new javax.swing.border.EtchedBorder());
          jPanel1.setMaximumSize(new java.awt.Dimension(32767, 35));
          lblImageCoord.setText("(999, 999)");
          lblImageCoord.setToolTipText("Pixel coordinates");
          jPanel1.add(lblImageCoord);

          jLabel1.setText("==>");
          jPanel1.add(jLabel1);

          eWorldX.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
          eWorldX.setText("0.0");
          eWorldX.setToolTipText("Real world coordinates");
          eWorldX.setMinimumSize(new java.awt.Dimension(70, 20));
          eWorldX.setPreferredSize(new java.awt.Dimension(70, 20));
          jPanel1.add(eWorldX);

          eWorldY.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
          eWorldY.setText("0.0");
          eWorldY.setToolTipText("Real world coordinates");
          eWorldY.setMinimumSize(new java.awt.Dimension(70, 20));
          eWorldY.setPreferredSize(new java.awt.Dimension(70, 20));
          jPanel1.add(eWorldY);

          getContentPane().add(jPanel1);

          jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

          jPanel2.setMaximumSize(new java.awt.Dimension(32767, 36));
          btnOK.setText("OK");
          btnOK.setSelected(true);
          btnOK.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnOKActionPerformed(evt);
               }
          });

          jPanel2.add(btnOK);

          btnCancel.setText("Cancel");
          btnCancel.setDefaultCapable(false);
          btnCancel.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnCancelActionPerformed(evt);
               }
          });

          jPanel2.add(btnCancel);

          getContentPane().add(jPanel2);

          setBounds(0, 0, 414, 510);
     }//GEN-END:initComponents

	private void pnlZoomedImageMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlZoomedImageMouseClicked
		// Add your handling code here:
		Point mouse = new Point((int)evt.getX(), (int)evt.getY());
		//System.out.println(mouse);
		if (mouse.x < this.subImage.getWidth()*this.scale && mouse.y < this.subImage.getHeight()*this.scale){
			this.chip.clearPoints();
			this.chip.addPoint(mouse);
			Point2D zoomed = this.getZoomedPoint(mouse.x, mouse.y);
			this.lblImageCoord.setText("(" + zoomed.getX() + ", " + zoomed.getY() + ")");
			this.xImage = zoomed.getX();
			this.yImage = zoomed.getY();
		}
	}//GEN-LAST:event_pnlZoomedImageMouseClicked

	private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
		// Add your handling code here:
		this.ok = false;
		this.closeDialog(null);
	}//GEN-LAST:event_btnCancelActionPerformed

	private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
		// Add your handling code here:
		this.ok = true;
		try{
			this.xWorld = new Double(this.eWorldX.getText()).doubleValue();
		}
		catch(NumberFormatException e){
			this.eWorldX.grabFocus();
		}
		try{
			this.yWorld = new Double(this.eWorldY.getText()).doubleValue();
			this.closeDialog(null);
		}
		catch(NumberFormatException e){
			this.eWorldY.grabFocus();
		}
	}//GEN-LAST:event_btnOKActionPerformed
	
	/** Closes the dialog */
	private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
		setVisible(false);
		dispose();
	}//GEN-LAST:event_closeDialog
		
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JButton btnCancel;
     private javax.swing.JButton btnOK;
     private javax.swing.JTextField eWorldX;
     private javax.swing.JTextField eWorldY;
     private javax.swing.JLabel jLabel1;
     private javax.swing.JPanel jPanel1;
     private javax.swing.JPanel jPanel2;
     private javax.swing.JLabel lblImageCoord;
     private javax.swing.JPanel pnlZoomedImage;
     // End of variables declaration//GEN-END:variables
	
}
